<?php
namespace app\index\controller;

use think\facade\View;
use think\facade\Db;
use think\facade\Request;
use think\facade\Session;

/**
 * 表结构生成辅助
 */
class Table extends Front
{
    public function index()
    {
    	$this->createTable();
        die;
    }
    public function createTable()
    {
    	$table_name = 'article';
    	$fields = [
    		['name'=>'id','type'=>'int','length'=>'11','default'=>'NOT NULL','other'=>'AUTO_INCREMENT','comment'=>'备注'],
    		['name'=>'max_number','type'=>'int','length'=>'11','default'=>'DEFAULT NULL','other'=>'','comment'=>'最大值']
    	];
    	$primary_key = 'id';
    	$sql = $this->parseTosql($table_name, $fields, $primary_key);
    	
    }
    public function parseTosql($table_name,$fields,$primary_key)
    {
    	foreach($fields as $k=>$v){
    		$fields[$k] = '`'.$v['name'].'`';
    		$fields[$k] .= ' '.$v['type']."(".$v['length'].")";
    		$fields[$k] .= ' '.$v['default'];
    		$fields[$k] .= $v['other']==''?'':' '.$v['other'];
    		$fields[$k] .= $v['comment']==''?'':' COMMENT "'.$v['comment'].'"';
    	}
    	if($primary_key!='')
    	{
    		$fields[] = 'PRIMARY KEY (`'.$primary_key.'`)';
    	}

    	$sql_table = 'CREATE TABLE `'.$table_name.'`';
    	$sql_fields = implode(',', $fields);
    	$sql_table_set = 'ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8;';
    	$sql = $sql_table.' ('.$sql_fields.') '.$sql_table_set;
    	return $sql;
    }
    public function fieldTypes()
    {
    	[
    		//整数
    		'tinyint',
    		'smallint',
    		'mediumint',
    		'int',
    		'bigint',
    		//小数
    		'float',
    		'Double',
    		'decimal',
    		//日期
    		'year',
    		'timestamp',
    		'time',
    		'date',
    		'datetime',
    		//字符串
    		'set',
    		'enum',
    		'blob',
    		'text',
    		'varchar',
    		'char'
    	];
    }
}
